package top.abilities.util.util;

import java.util.Random;

/**
 * 随机相关操作
 *
 **/
public class RadomUtil {
    private static final String numberChar = "0123456789";
    public static final String allChar = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 返回指定长度的随机数字字符串
     * @param length 指定长度
     * @return 字符串
     */
    public static String genNumber(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(numberChar.charAt(random.nextInt(numberChar.length())));
        }
        return sb.toString();
    }

    /**
     * 返回指定长度的随机字符串（包括字母和数字）
     * @param length 指定长度
     * @return 字符串
     */
    public static String genAll(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        return sb.toString();
    }
}
